package com.kylewang.pomodoro.ui.plan;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.bytedance.scene.group.GroupScene;
import com.kylewang.pomodoro.MainActivity;
import com.kylewang.pomodoro.R;
import com.kylewang.pomodoro.helper.Utils;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.view.listener.OnInputClickListener;

public class PlanScene extends GroupScene {

    private ViewGroup mRootView;
    private ListView listView;
    private TaskAdapter adapter;

    @NonNull
    @Override
    public ViewGroup onCreateView(@NonNull LayoutInflater layoutInflater, @NonNull ViewGroup viewGroup, @Nullable Bundle bundle) {
        mRootView = (ViewGroup) layoutInflater.inflate(R.layout.fragment_plan, viewGroup, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MainActivity mainActivity = (MainActivity) getActivity();
        adapter = new TaskAdapter(mainActivity);
        listView = mRootView.findViewById(R.id.taskitemsList);
        listView.setAdapter(adapter);
        mainActivity.registerSubscribeAuthEvents(adapter);
        Button addTaskBtn = (Button) findViewById(R.id.btn_add_task);
        addTaskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Utils.isLogin()) {
                    ((MainActivity)getActivity()).login();
                    return;
                }
                CircleDialog.Builder dialog = new CircleDialog.Builder()
                        .setInputText("", "Please input your new task name")
                        .setInputHeight(40)
                        .setInputCounter(20)
                        .setInputShowKeyboard(true)
                        .setPositiveInput("Submit", new OnInputClickListener() {
                            @Override
                            public boolean onClick(String text, EditText editText) {
                                if (text == null || text.length() == 0) {
                                    return false;
                                }
                                adapter.addNewTask(text);
                                return true;
                            }
                        })
                        .setNegative("Cancel", null);
                FragmentManager manager = ((MainActivity)getActivity()).getSupportFragmentManager();
                dialog.show(manager);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.fetchTasks();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}